package studentsinfomanagement.view.student;

import studentsinfomanagement.bean.Student;
import studentsinfomanagement.dao.StudentDAO;
import studentsinfomanagement.main.Main;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

/**
 * 学生修改个人信息界面
、
 */
public class StudentEditView extends JFrame {

    private StudentEditView studentEditView = null;
    private Main main = null;//逻辑层对象
    private Student student = null;//学生信息

    public StudentEditView(Main main, Student student){
        studentEditView = this;
        this.main = main;
        this.student = student;

        this.setTitle("修改个人信息");
        this.setSize(350,400);//大小
        this.setLocationRelativeTo(null);//居中
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);//关闭界面取消操作
        this.setResizable(false);//大小不可调节

        GridLayout gl = new GridLayout();//网格布局
        gl.setRows(7);
        gl.setColumns(1);
        this.setLayout(gl);

        //界面元素
        JLabel jl_title = new JLabel("修改个人信息");//标题
        jl_title.setFont(new Font("Dialog",1,15));//设置字体

        JLabel jl_name = new JLabel("姓名：");//姓名
        JTextArea jt_name = new JTextArea();
        jt_name.setText(this.student.getName());
        jt_name.setEditable(false);

        JLabel jl_sex = new JLabel("性别");//性别
        JTextArea jt_sex = new JTextArea();
        jt_sex.setText(this.student.getSex());
        jt_sex.setEditable(false);

        JLabel jl_dep = new JLabel("专业：");//专业
        JTextArea jt_dep = new JTextArea();
        jt_dep.setText(this.student.getDepartment());
        jt_dep.setEditable(false);

        JLabel jl_pwd_old = new JLabel("原始密码：");//专业
        JTextField jt_pwd_old = new JTextField();
        jt_pwd_old.setPreferredSize(new Dimension(250,25));

        JLabel jl_pwd_new = new JLabel("更新密码：");//专业
        JTextField jt_pwd_new = new JTextField();
        jt_pwd_new.setPreferredSize(new Dimension(250,25));

        JButton btn_next = new JButton("确认修改");//确认修改个人信息
        btn_next.setPreferredSize(new Dimension(150,25));
        JButton btn_back = new JButton("取消");//取消操作
        btn_next.setPreferredSize(new Dimension(150,25));

        //标题区域
        JPanel jp_title = new JPanel();
        jp_title.add(jl_title);
        this.add(jp_title);
        //姓名区域
        JPanel jp_name = new JPanel();
        jp_name.add(jl_name);
        jp_name.add(jt_name);
        this.add(jp_name);
        //性别区域
        JPanel jp_sex = new JPanel();
        jp_sex.add(jl_sex);
        jp_sex.add(jt_sex);
        this.add(jp_sex);
        //专业区域
        JPanel jp_dep = new JPanel();
        jp_dep.add(jl_dep);
        jp_dep.add(jt_dep);
        this.add(jp_dep);
        //可以修改的密码区域
        JPanel jp_pwd_old = new JPanel();
        jp_pwd_old.add(jl_pwd_old);
        jp_pwd_old.add(jt_pwd_old);
        this.add(jp_pwd_old);
        JPanel jp_pwd_new = new JPanel();
        jp_pwd_new.add(jl_pwd_new);
        jp_pwd_new.add(jt_pwd_new);
        this.add(jp_pwd_new);
        //按钮区域
        JPanel jp_btn = new JPanel();
        jp_btn.add(btn_next);
        jp_btn.add(btn_back);
        this.add(jp_btn);

        //设置界面可见
        this.setVisible(true);

        //为按钮添加监听器
        //确认按钮
        btn_next.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                //获取旧密码
                String old_pwd = jt_pwd_old.getText().trim();
                //获取新密码
                String new_pwd = jt_pwd_new.getText().trim();
                //判断输入是否为空
                if(old_pwd == null || new_pwd == null){
                    JOptionPane.showMessageDialog(studentEditView,"请输入原始密码和新密码");
                }else {
                    //检查旧密码是否正确
                    if (old_pwd.equals(student.getPassword())) {

                        //更新学生信息
                        student.setPassword(new_pwd);
                        StudentDAO studentDAO = StudentDAO.getInstance();
                        boolean result = studentDAO.update(student);
                        if(!result){
                            JOptionPane.showMessageDialog(studentEditView,"修改失败！");
                        }else{//修改成功
                            JOptionPane.showMessageDialog(studentEditView,"密码修改成功！");
                            studentEditView.setVisible(false);
                        }
                    } else {//旧密码错误
                        JOptionPane.showMessageDialog(studentEditView, "原始密码错误！");
                    }
                }
            }

            @Override
            public void mousePressed(MouseEvent e) {

            }

            @Override
            public void mouseReleased(MouseEvent e) {

            }

            @Override
            public void mouseEntered(MouseEvent e) {

            }

            @Override
            public void mouseExited(MouseEvent e) {

            }
        });

        //取消按钮
        btn_back.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                studentEditView.setVisible(false);
            }

            @Override
            public void mousePressed(MouseEvent e) {

            }

            @Override
            public void mouseReleased(MouseEvent e) {

            }

            @Override
            public void mouseEntered(MouseEvent e) {

            }

            @Override
            public void mouseExited(MouseEvent e) {

            }
        });

    }

}
